package com.eagle;

import com.eagle.constant.Constant;
import com.eagle.entity.ExcelTemplate;
import com.eagle.entity.MavenDependency;
import com.eagle.util.FileUtil;
import com.eagle.util.PropertiesUtil;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.Set;

/**
 * @author qinlinsen
 */
public class GenerateRunner {
    private static final String department = PropertiesUtil.getValueFromKey(Constant.DEPARTMENT);
    private static final String principal = PropertiesUtil.getValueFromKey(Constant.PRINCIPAL);
    private static final String systemName = PropertiesUtil.getValueFromKey(Constant.SYSTEM_NAME);

    private static final String sourceFileAbsoluteFilePath = PropertiesUtil.getValueFromKey(Constant.SOURCE_FILE_ABSOLUTE_PATH);
    private static final String templateFileAbsolutePath = PropertiesUtil.getValueFromKey(Constant.TEMPLATE_EXCEL_FILE_ABSOLUTE_PATH);

    public static void main(String[] args) {
        final String generateFileAbsolutePath = getGenerateFileAbsolutePath(systemName + "_依赖统计.xlsx");
        //copy template.xlsx
        cn.hutool.core.io.FileUtil.copy(templateFileAbsolutePath, generateFileAbsolutePath, true);
        ExcelTemplate excelTemplate = initExcelTemplate();
        try {
            final Set<MavenDependency> originDatum = FileUtil.readFileFrom(Paths.get(sourceFileAbsoluteFilePath));
            FileUtil.writeToTemplateExcel(originDatum,
                    "Sheet1",
                    generateFileAbsolutePath,
                    2,
                    excelTemplate);
            System.out.println(String.format("%s 文件生成成功", generateFileAbsolutePath));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(String.format("%s 文件生成失败", generateFileAbsolutePath));
        } finally {
            System.out.println("程序执行完毕");
        }
    }

    private static ExcelTemplate initExcelTemplate() {

        final ExcelTemplate excelTemplate = new ExcelTemplate();
        excelTemplate.setDepartment(department);
        excelTemplate.setPrincipal(principal);
        excelTemplate.setSystemName(systemName);
        return excelTemplate;
    }


    private static String getGenerateFileAbsolutePath(final String generateFileName) {
        Objects.requireNonNull(generateFileName);
        StringBuilder appender = new StringBuilder();
        String generateExcelFileDirectory = PropertiesUtil.getValueFromKey(Constant.GENERATOR_EXCEL_FILE_ABSOLUTE_DIRECTORY);
        final String generateFileAbsolutePath = appender.append(generateExcelFileDirectory)
                .append(File.separator)
                .append(generateFileName)
                .toString();
        return generateFileAbsolutePath;
    }


}
